/*
	File:	sys/Settings.hpp
	Date:	2013-06-30
	Author:	Asteroth
*/


#ifndef __ZEN_SETTINGS__
#define __ZEN_SETTINGS__

#include <sys/stat.h>
#include <JSL.hpp>

#include "Resources.hpp"
#include "../debug.hpp"

namespace zen {

	namespace sys {

		class Settings {

		public:

			Settings(): settings(NULL) { };
			Settings(const std::string &resource): settings(NULL) { load(resource); };
			~Settings() { free(); };

			bool isLoaded() const { return settings != NULL; }

			void set(const std::string &property, const std::string &value)		{ settings->set(property, value); }
			void setInt(const std::string &property, int value)					{ settings->set(property, value); }
			void setFloat(const std::string &property, float value)				{ settings->set(property, value); }
			void setBool(const std::string &property, bool value)				{ settings->set(property, value); }
			void setNull(const std::string &property)							{ settings->setNull(); }

			std::string get(const std::string &property) const					{ return settings->path(property)->value(); }
			int getInt(const std::string &property) const						{ return settings->path(property)->asInt(); }
			float getFloat(const std::string &property) const					{ return settings->path(property)->asFloat(); }
			bool getBool(const std::string &property) const						{ return settings->path(property)->asBool(); }
			bool isNull(const std::string &property) const						{ return settings->path(property)->isNull(); }

			jsl::Node* getNode(const std::string &property)						{ return settings->path(property); }
			jsl::Node* getNode(const std::string &property)	const				{ return settings->path(property); }

			bool load(const std::string &resource) {
				zen_call("Settings::load");
				free();
				settings = Resources::load(resource);
				zen_if_log(settings == NULL, "Error: Failed to parse settings file\"" + resource +  "\".");
				return settings != NULL;
			}

			bool save(const std::string &resource) {
				zen_call("Settings::save");
				bool saved = Resources::save(settings, resource);
				zen_if_log(!saved, "Error: Failed to save settings file\"" + resource +  "\".");
				return saved;
			}

			void free() {
				if (settings != NULL) {
					delete settings;
					settings = NULL;
				}
			}

			
		private:

			jsl::Node* settings;

		};

	} // namespace sys

} // namespace zen

#endif
